訓練 LoRA (Low-Rank Adaptation) 模型是一個有效的技術,可以在不大幅度改變原有模型的情況下,針對特定任務進行微調。這裡是使用 Stable Diffusion WebUI 訓練 LoRA 模型的詳細步驟
首先,確保你的環境已經安裝並配置了 Stable Diffusion WebUI 和所需的依賴項。如果還未安裝,可以參考以下命令:
安裝依賴項
pip install torch torchvision transformers diffusers
訓練 LoRA 模型需要準備合適的數據集。這些數據集應該包含你希望模型學習的新內容或風格。數據集可以是圖像對(圖像和其對應的描述文本)。
git clone https://github.com/derrian-distro/LoRA_Easy_Training_Scripts.git
#複製儲存庫
cd LoRA_Easy_Training_Scripts
git submodule init
git submodule update
cd sd_scripts
pip install torch torchvision --extra-index-url https://download.pytorch.org/whl/cu116
pip install --upgrade -r requirements.txt
pip install -U xformers
新增訓練設定檔trainingconfig.json
vim trainingconfig.json
填入以下內容(雙斜線的註解記得刪除) LoRA的總訓練步數計算公式為: 訓練圖片數量 × 重複次數 ÷ train_batch_size × epoch
{
//基於何種模型訓練
"pretrained_model_name_or_path": "/home/user/桌面/heralora/anything-v4.5-pruned.ckpt",
"v2": false,
"v_parameterization": false,
//紀錄檔輸出目錄
"logging_dir": "/home/user/桌面/heralora/log_dir/",
//訓練資料目錄
"train_data_dir": "/home/user/桌面/heralora/image_dir/",
//註冊目錄
"reg_data_dir": "/home/user/桌面/heralora/reg_dir/",
//輸出目錄
"output_dir": "/home/user/桌面/heralora/output_dir",
//訓練的圖片最大長寬
"max_resolution": "512,512",
//學習率
"learning_rate": "1e-5",
"lr_scheduler": "constant_with_warmup",
"lr_warmup": "5",
"train_batch_size": 3,
//訓練時期
"epoch": "4",
"save_every_n_epochs": "",
"mixed_precision": "fp16",
"save_precision": "fp16",
"seed": "",
"num_cpu_threads_per_process": 32,
"cache_latents": true,
"caption_extension": ".txt",
"enable_bucket": true,
"gradient_checkpointing": false,
"full_fp16": false,
"no_token_padding": false,
"stop_text_encoder_training": 0,
"use_8bit_adam": true,
"xformers": true,
"save_model_as": "safetensors",
"shuffle_caption": true,
"save_state": false,
"resume": "",
"prior_loss_weight": 1.0,
"text_encoder_lr": "1.5e-5",
"unet_lr": "1.5e-4",
"network_dim": 128,
"lora_network_weights": "",
"color_aug": false,
"flip_aug": false,
"clip_skip": 2,
"mem_eff_attn": false,
"output_name": "",
"model_list": "",
"max_token_length": "150",
"max_train_epochs": "",
"max_data_loader_n_workers": "",
"network_alpha": 128,
"training_comment": "",
"keep_tokens": 2,
"lr_scheduler_num_cycles": "",
"lr_scheduler_power": "",
"persistent_data_loader_workers": true,
"bucket_no_upscale": true,
"random_crop": false,
"caption_dropout_every_n_epochs": 0.0,
"caption_dropout_rate": 0
}